/*global define, console */

define(['angular', 'require', 'base'], function (angular, require, base) {
    "use strict";

    var factory = function ($rootScope, $http,$ionicLoading, CommonService, LocalCacheService) {

        var type = 2; //小财迷分类

        return {
            /**
             * 查询根栏目  fdxqszg 9  otcfdsy 5
             */
            getChannelInfosByRoot : function(){
                return _ajax("/prod/catalog/catalogs", {tree_type: type, catalog_level:1});
            },
            /**
             * 查询子栏目
             */
            getChannelInfosByParentId : function(channel_id){
                return _ajax("/prod/catalog/child_catalogs", {parent_catalog_id : channel_id});
            },
            /**
             * 查询分类详情
             */
            getChannelInfo : function(channel_id){
                return _ajax("/prod/catalog/catalog_detail", {catalog_id : channel_id});
            },
            /**
             * 查询产品
             */
            getProductsByChannelId : function(channel_id){
                return _ajax("/prod/list/prods", {catalog_id : channel_id, tree_type : type});
            },
            /**
             * 查询栏目下的otc产品
             */
            getOtcProductsByChannelId : function(channel_id){
                return _ajax("/prod/list/otc_prods", {catalog_id : channel_id, tree_type : type});
            },
            /**
             * 批量产品概要 产品代码查询
             */
            getProdBriefs:function(prod_codes){
                return _ajax("/prod/list/prods", {prod_codes: prod_codes});
            },
            getOtcProducts : function(prod_codes) {
                return _ajax("/prod/list/otc_prods", {prod_codes: prod_codes});
            },
            /**
             * 查询指数收益
             */
            getIndexProfit:function(indexCodes){
                return _ajax("/prod/history/index_profit", {index_code : indexCodes});
            },
            /**
             * 查询指数历史数据
             */
            getHistoryIndex:function(indexCode,begin_date,end_date){
                if(begin_date && begin_date.length == 8) {
                    return _ajax("/prod/history/index",{index_code: indexCode, begin_date:begin_date, end_date : end_date});
                } else {
                    return _ajax("/prod/history/index",{index_code: indexCode, count:begin_date});
                }
            },
            /**
             * 查询子栏目产品
             */
            getChildCatalogProds:function(channel_id){
                return _ajax("/prod/list/childcatalog_prods",  {parent_catalog_id : channel_id, tree_type : type});
            },
            /**
             * 获取OTC产品概况
             */
            getOTCGeneral: function (prod_code) {
                return _ajax("/prod/single/otc", {prod_code: prod_code});
            },
            /**
             * 获取产品概况
             */
            getFundGeneral: function (prod_code) {
                return _ajax("/prod/single/prod", {prod_code: prod_code});
            },
            /**
             * 查询OTC明细
             */
            getOTCDetail:function (prod_code){
                return _ajax("/prod/single/otc_detail", {prod_code: prod_code});
            },
            /**
             * 查询基金明细
             */
            getContentDetail:function (prod_code){
                return _ajax("/prod/single/prod_detail", {prod_code: prod_code});
            },
            /**
             * 查询货基七日年化
             */
            getHistoryYield:function (prod_id,count){
                return _ajax("/prod/history/yield",{prod_id: prod_id, count:count});
            },
            /**
             * 基金净值，沪深300指数历史数据
             */
            getComparisonData:function (prod_id, count){
                return _ajax("/prod/history/csi300_nav",{prod_id: prod_id, count:count});
            },
            /**
             * 查询基金历史净值数据
             */
            getHistoryNav:function (prod_id,count){
                return _ajax("/prod/history/nav",{prod_id: prod_id, count:count});
            },
            /**
             * 获取推荐的关注列表
             */
            getOthersCare:function(){
                return _ajax("/prod/list/others_care");
            },
            /**
             * 短债产品可赎回查询
             */
            getShortTermFund:function(prod_code,trans_account){
                return _ajax("/prod/single/short_sell", {prod_code: prod_code, trans_account:trans_account});
            },
            /**
             * 根据输入日期查询相关收益日期
             */
            getProfitDate:function(prod_code,entrust_date){
                return _ajax("/prod/single/profit_date", {prod_code: prod_code, entrust_date:entrust_date});
            },
            /**
             * 获取产品文件列表
             */
            getProdFiles: function(prod_id){
                return _ajax("/prod/single/prod_files", {prod_id: prod_id});
            },
            /**
             * 获取产品文件详情
             */
            getProdFileContent: function(file_id){
                return _ajax("/prod/single/prod_file/detail", {file_id: file_id});
            },
            /**
             * 根据主题标示 获取主题下的产品列表
             */
            getThemeDetail:function(theme_mark){
                return _ajax("/prod/single/prod_theme/detail", {theme_mark: theme_mark});
            },
            /**
             * 根据主题标示 获取主题下的产品列表
             */
            getProdByTheme:function(theme_mark,relate_type,order_rule){
                return _ajax("/prod/single/prod_theme/prods", {theme_mark: theme_mark,relate_type:relate_type,order_rule:order_rule});
            }
        };

        /**
         * 内部调用ajax方法
         * @param url
         * @private
         */
        function _ajax(url,data){
            var params = {};
            params.url = url;
            params.type = "get";
            params.cache = true;
            if(data) {
                params.data = data;
            }
            return CommonService.ajax(params);
        }
    };

    factory.$inject = ['$rootScope', '$http','$ionicLoading', 'CommonService', 'LocalCacheService'];
    return factory;
});